package com.sf.marathon.soul.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.sf.marathon.soul.core.base.BaseService;
import com.sf.marathon.soul.dao.ExpressGroupDetailMapper;
import com.sf.marathon.soul.domain.ProMarketBannerImg;
import com.sf.marathon.soul.vo.ExpressGroupDetail;
import com.sf.marathon.soul.vo.GroupUser;

/**
 * <p>
 * 拼团明细服务
 * </p>
 *
 * @author 325175
 * @since 2017-12-15
 */
@Service
public class ExpressGroupDetailService extends BaseService<ExpressGroupDetailMapper, ExpressGroupDetail> {

    @Value("${my.imageUrlPrefix}")
    private String imageUrlPrefix;

    private final static String MARKET = "market/";

    private final static String USER = "user/";

    @Autowired
    ProMarketBannerImgService imgService;

    @Autowired
    private UserService userService;

    public ExpressGroupDetail findOneById(Long groupId) {
        ExpressGroupDetail d = super.baseDao.findOneById(groupId);
        if (d != null) {
            List<ProMarketBannerImg> imgs = imgService.findByMarketId(d.getMarketId());
            List<String> imgUrls = new ArrayList<>();
            for (ProMarketBannerImg i : imgs) {
                imgUrls.add(imageUrlPrefix + MARKET + i.getBannerName());
            }
            d.setBannarImgs(imgUrls);

            // set users
            List<GroupUser> users = super.baseDao.findGroupUserByGroupId(groupId);
            for (GroupUser u : users) {
                u.setPhotoUrl(imageUrlPrefix + USER + u.getPhoto());
            }
            d.setUsers(users);

            d.setJoinNum(users.size());
            d.setLeftNum(d.getGroupLimit()-d.getJoinNum());
        }
        return d;
    }

}
